package servlet;

import pojo.User;
import service.Userservice;
import service.impl.UserServiceimpl;
import utils.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/user")
public class UserServlet extends HttpServlet {
    Userservice userservice = new UserServiceimpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Userservice userservice = new UserServiceimpl();
        String action = req.getParameter("action");
        req.setCharacterEncoding("UTF-8");
        //判断action来执行操作
        if("list".equals(action)){
            String name = req.getParameter("name");
            String pass = req.getParameter("pass");

            User condition = new User(name, pass);

            String currentPage =req.getParameter("currentPage");//没传是null
            if (currentPage == null)
            {
                currentPage = "1";
            }
            int int_currentPage = Integer.parseInt(currentPage);
            Page page = new Page();
            page.setCurrentPage(int_currentPage);
            //
            int count=userservice.count(condition);
            page.setTotalCount(count);
            page.setTotalPage();
            //计算偏移量
            int pageSize = page.getPageSize();
            int offset = (int_currentPage - 1) * page.getPageSize();
            List<User> users = userservice.findAll(condition, offset, pageSize);
            // 把数据放到请求域中
            req.setAttribute("users", users);
            req.setAttribute("condition", condition);
            req.setAttribute("page", page);
            req.getRequestDispatcher("/list.jsp").forward(req, resp);
        }
        else if("delete".equals(action)){
            String sid= req.getParameter("id");
            Integer id=Integer.parseInt(sid);
            int result = userservice.DeleteUserById(id);
            if (result > 0) {
                // 重定向到查询所有用户的页面
                resp.sendRedirect("user?action=list");
            }else {}

        }
        else if("add".equals(action)){
            String name = req.getParameter("name");
            String pass= req.getParameter("pass");
            int result = userservice.addUser(new User(name,pass));
            if(result>0){
                resp.sendRedirect("user?action=list");
            }
            else {
                resp.sendRedirect("addUser.jsp");
            }
        }
        else if("update".equals(action)){
            String sid = req.getParameter("id");
            Integer id = Integer.parseInt(sid);
            // 拿到网页传递过来的用户名和密码
            String name = req.getParameter("name");
            String pass = req.getParameter("pass");

            int result = userservice.UpdateUser(new User(id,name,pass));
            if (result>0) {
                resp.sendRedirect("user?action=list");
            }
            else{
            }
        }
        else if("toUpdate".equals(action)){

            // 设置请求字符

            // 拿到网页传递过来的用户名和密码
            String sid = req.getParameter("id");

            Integer id = Integer.parseInt(sid);

            // 根据id查询用户信息
            User user = userservice.findById(id);

            // 将查询到的用户信息存储到作用域中
            req.setAttribute("user", user);
            req.getRequestDispatcher("UpdateUser.jsp").forward(req, resp);
        }
    }

}
